<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ujian extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('login_model','l_db');
		$this->load->model('ujian_model','u_db');
	}

	/**
	 * Index Page for this controller.
	 */
	public function index($bab_id = false)
	{
		if($this->session->userdata('username')){			
			if($this->session->userdata('timestart_'.$bab_id)){
				$timepassed = time() - $this->session->userdata('timestart_'.$bab_id);
			}else{
				$this->session->set_userdata(array('timestart_'.$bab_id => time()));
				$timepassed = 0;
			}
			$bab 					= $this->u_db->get_bab($bab_id);
			if($bab){
			 	$bab 				= $bab->row_array();			
				$header['soal'] 	= $this->u_db->get_list($bab_id);
				$header['bab_id']	= $bab_id;
				$data['username'] 	= $this->session->userdata('username');			
				$longWayOff 		= $bab['time_limit'] - $timepassed;
				$footer['js'][] = '<script> var timeLeft = '.$longWayOff.';</script>';
				$footer['js'][] = "<script type='text/javascript'>var base_url = '".base_url().index_page()."';</script>";
				$footer['js'][] = '<script src="'.app_asset_url().'js/jquery.countdown.js"></script>';
				$footer['js'][] = '<script src="'.app_asset_url().'js/app/ujian.js"></script>';
				$this->load->view('header_ujian_view',$header);
				$this->load->view('ujian_view',$data);
				$this->load->view('footer_view',$footer);
			}else{
				$this->session->set_flashdata( 'message', array( 'title' => 'Error', 'content' => 'Invalid BAB ID !', 'type' => 'message' )); 
				redirect();
			}
		}else{
			$this->session->set_flashdata( 'message', array( 'title' => 'Error', 'content' => 'Please login !', 'type' => 'message' )); 
			redirect();
		}
	}
	

	public function save($bab_id = false){
		if($this->session->userdata('username')){
			if($bab_id){
				$hasil['uj_user_id'] = $this->session->userdata('id_user');	
				$hasil['uj_jd_id']	 = $bab_id;
				$hasil['uj_selesai'] = date('Y-m-d h:i:s');
				$jawaban = $this->u_db->get_list($bab_id);
				if($jawaban && $jawaban->num_rows() > 0){
					$jumlah_soal = $jawaban->num_rows();
					$hasil['uj_benar']= 0;
					foreach($jawaban->result_array() as $jwb){
						if(isset($_POST['jawaban'][$jwb['id_soal']]) && $_POST['jawaban'][$jwb['id_soal']] == $jwb['jawaban']) $hasil['uj_benar']++;
					}
					$hasil['uj_salah'] = $jumlah_soal - $hasil['uj_benar'];
					$hasil['uj_score'] = round($hasil['uj_benar']/$jumlah_soal*100,1);
					$hasil['uj_mulai'] = date('Y-m-d h:i:s',$this->session->userdata('timestart_'.$bab_id));
					if($this->u_db->save($hasil)){							
						$header['hasil']	= $hasil;
						$data['username'] 	= $this->session->userdata('username');			
						$this->load->view('header_ujian_view',$header);
						$this->load->view('hasil_ujian_view');
						$this->load->view('footer_view');
					}else{
						$this->session->set_flashdata( 'message', array( 'title' => 'Error', 'content' => 'Gagal tersimpan !', 'type' => 'message' )); 
						redirect();
					}
				}else{
					$this->session->set_flashdata( 'message', array( 'title' => 'Error', 'content' => 'Invalid Soal !', 'type' => 'message' )); 
					redirect();
				}
			}else{
				$this->session->set_flashdata( 'message', array( 'title' => 'Error', 'content' => 'Invalid BAB ID !', 'type' => 'message' )); 
				redirect();
			}
		}else{
			$this->session->set_flashdata( 'message', array( 'title' => 'Error', 'content' => 'Please login !', 'type' => 'message' )); 
			redirect();
		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */